package com.web.ocean.controller;

import com.web.ocean.entity.Ebook;
import com.web.ocean.service.IEbookService;
import com.web.ocean.utils.CommonResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 电子书 模块 服务api（接口）
 * 测试：1.工具   2.在线文档
 * </p>
 *
 * @author huangwenshan
 * @since 2024-12-17
 */
@Api(tags = "电子书服务模块")
@RestController
@RequestMapping("/ebook")
@CrossOrigin
public class EbookController {
    @Autowired
    private IEbookService ebookService;

    //查询
    @ApiOperation("查询所有电子书信息")
    @GetMapping("/findAll")
    public CommonResp findAll(){
        List<Ebook> list =ebookService.list();
        return new CommonResp(true,"查询所有的电子书",list);
    }

    @ApiOperation("根据id查询单条电子书信息")
    @GetMapping("/findOne/{id}")
    public CommonResp findOne(@PathVariable Long id){
         Ebook ebook=ebookService.getById(id);
         return new CommonResp(true,"查询单条电子信息",ebook);
    }
    //增改
    @ApiOperation("新增和编辑电子书信息")
    @PostMapping("save")
    public CommonResp save(@RequestBody @ApiParam("电子书对象") Ebook ebook){
        boolean result = ebookService.saveOrUpdate(ebook);
        if(result){
            return new CommonResp(true,"保存成功",null);
        }else {
            return new CommonResp(false,"保存失败",null);
        }
    }

    @ApiOperation("根据id删除单条电子书信息")
    @DeleteMapping("/remove/{id}")
    public CommonResp remove(@PathVariable Long id){
        boolean result = ebookService.removeById(id);
        if(result){
            return new CommonResp(true,"删除成功",null);
        }else {
            return new CommonResp(false,"删除失败",null);
        }
    }
}
